import { _decorator, Component, director, find, Label, Node } from 'cc';
import { SSS_GameData } from "../Data/SSS_GameData";
import CommonFunction from "../Utils/CommonFunction";
import { SSS_AudioManager } from '../Manager/SSS_AudioManager';
import { SSS_Consts } from '../Data/SSS_Consts';

const { ccclass, property } = _decorator;

@ccclass('MainPanel')
export default class MainPanel extends Component {
    public static Instance: MainPanel;
    @property(Node)
    public Root_main: Node | null = null;
    @property(Node)
    public Button_back: Node | null = null;

    onLoad() {
        MainPanel.Instance = this;
    }
    ShowOn() {
        this.node.active = true;
        this.Init();
        this.AddListener();
        this.Refresh();
    }
    ShowOff() {
        // this.node.destroy();
        this.RemoveListener();
        this.node.active = false;
    }
    Init() {
        CommonFunction.SetAllChildrenAtive(this.Root_main, false);
        for (let index = 0; index < SSS_GameData.list.length; index++) {
            const item_data = SSS_GameData.list[index];
            // console.log(`item_data`, item_data);

            // 根据item_data.name 获取场景中name一样的node
            let item_level = find(`${item_data.name}`, this.Root_main);
            // console.log(`item_level`, item_level);

            // const item_level = CommonFunction.GetFreeChild(this.Root_main);
            item_level.active = true;
            let Label_name: Label = find(`main/Label_name`, item_level).getComponent(Label);
            // let icon: Sprite = find(`Background/icon`, item_level).getComponent(Sprite);
            Label_name.string = `${item_data.name}`;
            // Label_name.node.active = false;

            let 线: Node = find(`main/线`, item_level);
            // 线.active = false;

            // bdlMgr.getRes<cc.SpriteFrame>(SSS_Consts.BundleName.SPRITE, name2, cc.SpriteFrame).then(res => {
            //     // console.log(`res`, res);
            //     icon.spriteFrame = res;
            //     icon.sizeMode = cc.Sprite.SizeMode.TRIMMED;
            //     // icon.node.scale = 1
            //     CommonFunction.SetFrame(icon, 80, 80);
            // });

            item_level.on(Node.EventType.TOUCH_END, () => {
                // console.log(item_data.name);

                // { name: `五花肉`, game: `五花肉`, icon: `五花肉`, scene: SSS_Consts.SceneName.五花肉 },

                this.点击游戏(item_data);

                // cc.director.loadScene(item_data.scene);

                // GameData_DanCiPeiDui.CurrentLevelData = GameData_DanCiPeiDui.wordList[GameData_DanCiPeiDui.CurrentLevelIndex];
                // GameData_DanCiPeiDui.InitData();
                // GameData_DanCiPeiDui.CurrentLevelIndex = index;
                // GameData_DanCiPeiDui.LevelData();
                // cc.director.loadScene(SSS_Consts.SceneName.DanCiPeiDui_Game_Scene);

                SSS_AudioManager.PlayButtonAudio();
            }, this);
        }

        // {
        //     const item_data = SSS_GameData.list[0];
        //     this.点击游戏(item_data);
        // }
    }
    Refresh() {
    }
    点击游戏(item_data) {
        director.loadScene(item_data.scene);
    }
    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
    }
    RemoveListener() {
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
    }
    On_Button_back() {
        //        // console.log("On_Button_back---");
        this.ShowOff();
        SSS_AudioManager.PlayButtonAudio();
    }
}